
class GameMap extends renderer.DisplayObjectContainer {

    grid: astar.Grid
    private config: any;
    public transmissionX: any;
    public transmissionY: any;
    
    public itemConfig: { [index: string]: Item } = {};
    private npcConfig: { [index: string]: Npc } = {};
    private monsterConfig:{[index: string]:monster} ={};

    private tileContainer = new renderer.DisplayObjectContainer();
    private itemContainer = new renderer.DisplayObjectContainer();
    private roleContainer = new renderer.DisplayObjectContainer();
    private monsterContainer=new renderer.DisplayObjectContainer();
    public musicAudio:HTMLAudioElement;

    public id = 0;
    // private image = new Image();
    private map_tiles: any[];

   


    

   

    
    constructor(config:any,Config:any) {
        super();
        this.id = config.id;
        this.musicAudio=document.createElement("audio");
        this.musicAudio.src=config.music;
        // console.log(this.MusicPosition);
        this.map_tiles = Config;
        console.log(this.map_tiles);
        
        this.grid = new astar.Grid(10,10);
        this.config = config.location;
        this.transmissionX = config.transmissionX;
        this.transmissionY = config.transmissionY;
       
        this.addChild(this.tileContainer);
        this.addChild(this.itemContainer);
        this.addChild(this.roleContainer);
        this.addChild(this.monsterContainer);


        this.drawNpc(npcManager);
        // this.drawItem(itemManager);
        this.drawmonster(monsterManager);

        for (let item of this.config) {
           const image=new Image()
                for (let map_tile of this.map_tiles) {
                    if (item.tile == map_tile.id) {
                        image.src = map_tile.image;
                    }
                   
                }
            
            const texture = image;
            const tile = new renderer.Bitmap(TILE_SIZE * item.x, TILE_SIZE * item.y, texture);
            // const walkable = item.tile == RIVER ? false : true;
            let walkable:boolean;
            if(item.tile==1||item.tile==2||item.tile==3||item.tile==4){
                walkable=true;
            }else{
                walkable=false;
            }
            this.grid.setWalkable(item.x, item.y, walkable);
            this.tileContainer.addChild(tile)


        }



    }
    

    addToRoleContainer(child: renderer.DisplayObject) {
        this.roleContainer.addChild(child);
    }
    removeRoleChilden() {
        this.roleContainer.removeChild(user.view);
    }

    getItemInfo(x: number, y: number) {
        return this.itemConfig[x + "_" + y];
    }

    getNpcInfo(x: number, y: number) {
        const key = x + "_" + y;
        return this.npcConfig[key]
    }
    getMonsterInfo(x: number, y: number){
        const key =x  + "_" +y;
        return this.monsterConfig[key];
    }
    removeDialog(mons:monster){
        const key = mons.x + "_" + mons.y;
        delete this.monsterConfig[key];
        this.removeChild(mons.view);



    }

    removeItem(item: Item) {
        console.log(9999);
       
        const key = item.x + "_" + item.y;
        delete this.itemConfig[key];

        this.itemContainer.removeChild(item.view);
    }
    removeMonster(monster:monster){
    const key=monster.x + "_"+ monster.y;
    delete this.monsterConfig[key];
    this.monsterContainer.removeChild(monster.view);

    }

    getTileNodeInfo(x: number, y: number) {
        for (let item of this.config) {
            if (item.x == x && item.y == y) {
                return item;
            }
        }
        return null;
    }

    drawNpc(npcManager: NpcManager) {
        for (let item of this.config) {
            for (let npc of npcManager.npc) {
                if (item.x == npc.x && item.y == npc.y&&this.id==npc.mapID) {
                    this.roleContainer.addChild(npc.view);
                        
                    npc.view.x =item.x * TILE_SIZE;
                    npc.view.y = npc.y * TILE_SIZE;
                   // npc.view.alpha = 0.4;
                    console.log (npc.view)
                    const key = npc.x + "_" + npc.y;
                    this.npcConfig[key] = npc;
                }
            }
        }
    }

    drawItem(itemManager: ItemManager,mon :monster) {
        for (let item1 of this.config) {
            for (let item of itemManager.Items) {
                if (item1.x == item.x && item1.y == item.y&&this.id==item.mapId&&item.id==mon.id) {
                    this.itemContainer.addChild(item.view);

                    item.view.x = item1.x * TILE_SIZE;
                    item.view.y = item1.y * TILE_SIZE;
                    const key = item.x + "_" + item.y;
                    this.itemConfig[key] = item;
                }
            }
        }
    }
    drawmonster(monsterManager: MonsterManager){
        for (let item of this.config){
            for (let item1 of monsterManager.Monsters){
                if (item.x ==item1.x && item.y == item1.y&&this.id==item1.mapID){
                   // console.log("viosjin!!!!");
                    this.monsterContainer.addChild(item1.view);

                    item1.view.x=item.x*TILE_SIZE;
                    item1.view.y= item.y* TILE_SIZE;
                    //monster.view.alpha=0.4;
                   // console.log(monster.view);
                    const key = item1.x + "_" + item1.y;
                    this.monsterConfig[key] = item1;

                }
            }

        }

    }
}

class MapManager{
    public maps: { [index: string]: GameMap } = {};
    private Id: string;
    private container: renderer.DisplayObjectContainer;
    // public musicAudio:HTMLAudioElement;

    parseFromConfig(id:string,config: any) {
        this.maps[id] = new GameMap(config.map[0], config.mapTile);
        
      
    }
    initContainer(container: renderer.DisplayObjectContainer) {
        this.container = container;
    }
    
    changeMap(mapId:string,x:number,y:number) {
        this.Id = mapId;
        const map = this.getMap();
        map.musicAudio.play();
        // this.musicAudio.src=map.MusicPosition;
        // // console.log(this.musicAudio.src)
        // this.musicAudio.play();
        this.container.removeAllChildren();
        map. removeRoleChilden();
        this.container.addChild(map);

       
       
        const mapStartNode = { x: 0, y: 0 };
        map.grid.setStartNode(x,y);
       
        map.addToRoleContainer(user.view);
        user.view.x = x * TILE_SIZE;
        user.view.y = y * TILE_SIZE;
        
        map.addEventListener("click", (eventData: any) => {
            const x = eventData.localX;
            const y = eventData.localY;
            const row = Math.floor(x / TILE_SIZE);
            const col = Math.floor(y / TILE_SIZE);


            const info = map.getTileNodeInfo(row, col);

            pool.clear();
            const walk = new WalkCommand(row, col);
            pool.add(walk);

            // if (user.view.x == 4*TILE_SIZE && user.view.y == 4*TILE_SIZE) {
            //     user.dispatchEvent("changeMap", { eventData: 111 });
            // }
    
          

            const npc = map.getNpcInfo(row, col);
            const monster =map.getMonsterInfo(row,col);

            if (npc) {
                const talk = new TalkCommand(npc);
                pool.add(talk)
            }

            const item = map.getItemInfo(row, col);

            if (item) {
                const pick = new PickCommand(item);
                pool.add(pick)
            }
            if (monster){
                const battle=new BattleCommand(monster);
                pool.add(battle);
            }



           pool.execute();

        })
        // if (this.Id != "map") {
        //      map.grid.setStartNode(4,4);
        // }
       

    }
    getMap() {
        
        return this.maps[this.Id];
    }
}

const mapManager = new MapManager();